<!-- 发布及采购 -->
<template>
  <basic-container>
    <avue-crud :before-open="beforeOpen"
               :data="data"
               :option="option"
               :page.sync="page"
               :permission="permissionList"
               :table-loading="loading"
               @current-change="currentChange"
               @on-load="onLoad"
               @refresh-change="refreshChange"
               @row-del="rowDel"
               @row-save="rowSave"
               @row-update="rowUpdate"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @size-change="sizeChange"
               ref="crud"
               v-model="form">
      <template slot="menuLeft">
        <avue-radio v-model="displayType" :dic="dic"></avue-radio>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {add, getDetail, getList, remove, update} from "@/api/enterprise/esmsproductcloud"; // 发布
import {mapGetters} from "vuex";
import {baseUrl} from "@/config/env";

export default {
  data() {
    return {
      displayType: 0, // 列表显示的列表内容 0:采购  1:发布
      dic: [{
        label: '采购列表',
        value: 0
      }, {
        label: '发布列表',
        value: 1
      }],
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        title: "发布/采购列表",
        height: 'auto',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        addBtn: true,
        viewBtn: true,
        delBtn: false,
        editBtn: false,
        refreshBtn: false,
        displayBtn: false,
        selection: false,
        menu: true,
        menuWidth: 130,
        columnBtn: false,
        dialogClickModal: false,
        column: [
          {
            label: "需求类型",
            prop: "functionType",
            type: 'select',
            hide: true,
            dicData: [
              {
                label: '采购产品',
                value: 0
              },
              {
                label: '发布产品',
                value: 1
              },
            ],
            rules: [{
              required: true,
              message: "请选择服务分类",
              trigger: "blur"
            }]
          },
          {
            label: "产品分类",
            prop: "type",
            type: 'select',
            dicUrl: baseUrl + "/blade-system/dict/dictionary?code=release_service_type",
            props: {
              label: "dictValue",
              value: "sort"
            },
            rules: [{
              required: true,
              message: "请选择产品分类",
              trigger: "blur"
            }]
          },
          {
            label: "产品名称",
            prop: "title",
            width: 250,
            overHidden: true,
            rules: [{
              required: true,
              message: "请填写产品名称",
              trigger: 'blur'
            }]
          },
          {
            label: "产品规格",
            prop: 'standards',
            rules: [{
              required: true,
              message: "请填写产品名称",
              trigger: 'blur'
            }]
          },
          {
            label: "需求数量",
            labelTip: "发布产品时无需填写",
            prop: 'demandQuantity',
            formatter(row, demandQuantity) {
              return demandQuantity <= 0 ? "暂无" : demandQuantity
            }
          },
          {
            label: "产品需求",
            labelTip: "发布产品时无需填写",
            span: 24,
            prop: 'demand',
            type: 'textarea',
            hide: true
          },
          {
            label: "产品简介",
            labelTip: "采购产品时无需填写",
            span: 24,
            prop: 'productIntroduce',
            type: 'textarea',
            hide: true
          },
          {
            label: "浏览量",
            prop: "views",
            display: false,
            formatter(row, views) {
              return views <= 0 ? "暂无" : views
            }
          },
          {
            label: "发布时间",
            prop: "updateTime",
            display: false,
            width: 150
          },
          {
            label: "审核状态",
            prop: "auditStatus",
            type: "select",
            display: false,
            dicUrl: baseUrl + "/blade-system/dict/dictionary?code=service_status",
            props: {
              label: "dictValue",
              value: "sort"
            }
          },
          {
            label: "图片",
            prop: "oneImageUrl",
            type: "upload",
            listType: 'picture-card',
            dataType: 'string',
            propsHttp: {
              res: 'data'
            },
            span: 24,
            tip: '只能上传jpg/png文件，且不超过500kb',
            action: baseUrl + '/blade-resource/local/endpoint/arrayFileUpload-file',
            hide: true,
          }
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.esmsproductcloud_add, false),
        viewBtn: this.vaildData(this.permission.esmsproductcloud_view, false),
        delBtn: this.vaildData(this.permission.esmsproductcloud_delete, false),
        editBtn: this.vaildData(this.permission.esmsproductcloud_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  watch: {
    displayType(n) {
      this.query = {
        functionType: n
      }
      this.onLoad({
        currentPage: 1,
        pageSize: this.page.pageSize
      })
    }
  },
  methods: {
    rowSave(row, done, loading) {
      add(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        loading();
        window.console.log(error);
      });
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        loading();
        window.console.log(error);
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      if (!this.query.functionType) {
        this.query.functionType = 0
      }
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style>
</style>
